{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import users_mgt as um"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "um.create_user_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "IntegrityError",
     "evalue": "(sqlite3.IntegrityError) UNIQUE constraint failed: user.email\n[SQL: INSERT INTO user (username, email, password) VALUES (?, ?, ?)]\n[parameters: ('test', 'test@test.com', 'sha256$56GMHIe8$b77d42b55c9fff8fe78b4aedba02ff2e280032642b4703417881bec64e28206f')]\n(Background on this error at: http://sqlalche.me/e/13/gkpj)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIntegrityError\u001b[0m                            Traceback (most recent call last)",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1276\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, *args)\u001b[0m\n\u001b[0;32m   1275\u001b[0m     \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m evt_handled:\n\u001b[1;32m-> 1276\u001b[0m         \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mdialect\u001b[39m.\u001b[39;49mdo_execute(\n\u001b[0;32m   1277\u001b[0m             cursor, statement, parameters, context\n\u001b[0;32m   1278\u001b[0m         )\n\u001b[0;32m   1280\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\default.py:608\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[1;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[0;32m    607\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mdo_execute\u001b[39m(\u001b[39mself\u001b[39m, cursor, statement, parameters, context\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m):\n\u001b[1;32m--> 608\u001b[0m     cursor\u001b[39m.\u001b[39;49mexecute(statement, parameters)\n",
      "\u001b[1;31mIntegrityError\u001b[0m: UNIQUE constraint failed: user.email",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[1;31mIntegrityError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mum\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_user\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtest\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtest1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtest@test.com\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\users_mgt.py:31\u001b[0m, in \u001b[0;36madd_user\u001b[1;34m(username, password, email)\u001b[0m\n\u001b[0;32m     27\u001b[0m ins \u001b[39m=\u001b[39m User_tbl\u001b[39m.\u001b[39minsert()\u001b[39m.\u001b[39mvalues(\n\u001b[0;32m     28\u001b[0m     username\u001b[39m=\u001b[39musername, email\u001b[39m=\u001b[39memail, password\u001b[39m=\u001b[39mhashed_password)\n\u001b[0;32m     30\u001b[0m conn \u001b[39m=\u001b[39m engine\u001b[39m.\u001b[39mconnect()\n\u001b[1;32m---> 31\u001b[0m conn\u001b[39m.\u001b[39;49mexecute(ins)\n\u001b[0;32m     32\u001b[0m conn\u001b[39m.\u001b[39mclose()\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1011\u001b[0m, in \u001b[0;36mConnection.execute\u001b[1;34m(self, object_, *multiparams, **params)\u001b[0m\n\u001b[0;32m   1007\u001b[0m     util\u001b[39m.\u001b[39mraise_(\n\u001b[0;32m   1008\u001b[0m         exc\u001b[39m.\u001b[39mObjectNotExecutableError(object_), replace_context\u001b[39m=\u001b[39merr\n\u001b[0;32m   1009\u001b[0m     )\n\u001b[0;32m   1010\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m-> 1011\u001b[0m     \u001b[39mreturn\u001b[39;00m meth(\u001b[39mself\u001b[39;49m, multiparams, params)\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\sql\\elements.py:298\u001b[0m, in \u001b[0;36mClauseElement._execute_on_connection\u001b[1;34m(self, connection, multiparams, params)\u001b[0m\n\u001b[0;32m    296\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_execute_on_connection\u001b[39m(\u001b[39mself\u001b[39m, connection, multiparams, params):\n\u001b[0;32m    297\u001b[0m     \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msupports_execution:\n\u001b[1;32m--> 298\u001b[0m         \u001b[39mreturn\u001b[39;00m connection\u001b[39m.\u001b[39;49m_execute_clauseelement(\u001b[39mself\u001b[39;49m, multiparams, params)\n\u001b[0;32m    299\u001b[0m     \u001b[39melse\u001b[39;00m:\n\u001b[0;32m    300\u001b[0m         \u001b[39mraise\u001b[39;00m exc\u001b[39m.\u001b[39mObjectNotExecutableError(\u001b[39mself\u001b[39m)\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1124\u001b[0m, in \u001b[0;36mConnection._execute_clauseelement\u001b[1;34m(self, elem, multiparams, params)\u001b[0m\n\u001b[0;32m   1114\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m   1115\u001b[0m     compiled_sql \u001b[39m=\u001b[39m elem\u001b[39m.\u001b[39mcompile(\n\u001b[0;32m   1116\u001b[0m         dialect\u001b[39m=\u001b[39mdialect,\n\u001b[0;32m   1117\u001b[0m         column_keys\u001b[39m=\u001b[39mkeys,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1121\u001b[0m         \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m,\n\u001b[0;32m   1122\u001b[0m     )\n\u001b[1;32m-> 1124\u001b[0m ret \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_execute_context(\n\u001b[0;32m   1125\u001b[0m     dialect,\n\u001b[0;32m   1126\u001b[0m     dialect\u001b[39m.\u001b[39;49mexecution_ctx_cls\u001b[39m.\u001b[39;49m_init_compiled,\n\u001b[0;32m   1127\u001b[0m     compiled_sql,\n\u001b[0;32m   1128\u001b[0m     distilled_params,\n\u001b[0;32m   1129\u001b[0m     compiled_sql,\n\u001b[0;32m   1130\u001b[0m     distilled_params,\n\u001b[0;32m   1131\u001b[0m )\n\u001b[0;32m   1132\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n\u001b[0;32m   1133\u001b[0m     \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdispatch\u001b[39m.\u001b[39mafter_execute(\u001b[39mself\u001b[39m, elem, multiparams, params, ret)\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1316\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, *args)\u001b[0m\n\u001b[0;32m   1313\u001b[0m             result\u001b[39m.\u001b[39m_autoclose_connection \u001b[39m=\u001b[39m \u001b[39mTrue\u001b[39;00m\n\u001b[0;32m   1315\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m-> 1316\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_handle_dbapi_exception(\n\u001b[0;32m   1317\u001b[0m         e, statement, parameters, cursor, context\n\u001b[0;32m   1318\u001b[0m     )\n\u001b[0;32m   1320\u001b[0m \u001b[39mreturn\u001b[39;00m result\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1510\u001b[0m, in \u001b[0;36mConnection._handle_dbapi_exception\u001b[1;34m(self, e, statement, parameters, cursor, context)\u001b[0m\n\u001b[0;32m   1508\u001b[0m     util\u001b[39m.\u001b[39mraise_(newraise, with_traceback\u001b[39m=\u001b[39mexc_info[\u001b[39m2\u001b[39m], from_\u001b[39m=\u001b[39me)\n\u001b[0;32m   1509\u001b[0m \u001b[39melif\u001b[39;00m should_wrap:\n\u001b[1;32m-> 1510\u001b[0m     util\u001b[39m.\u001b[39;49mraise_(\n\u001b[0;32m   1511\u001b[0m         sqlalchemy_exception, with_traceback\u001b[39m=\u001b[39;49mexc_info[\u001b[39m2\u001b[39;49m], from_\u001b[39m=\u001b[39;49me\n\u001b[0;32m   1512\u001b[0m     )\n\u001b[0;32m   1513\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m   1514\u001b[0m     util\u001b[39m.\u001b[39mraise_(exc_info[\u001b[39m1\u001b[39m], with_traceback\u001b[39m=\u001b[39mexc_info[\u001b[39m2\u001b[39m])\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\util\\compat.py:182\u001b[0m, in \u001b[0;36mraise_\u001b[1;34m(***failed resolving arguments***)\u001b[0m\n\u001b[0;32m    179\u001b[0m     exception\u001b[39m.\u001b[39m__cause__ \u001b[39m=\u001b[39m replace_context\n\u001b[0;32m    181\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m--> 182\u001b[0m     \u001b[39mraise\u001b[39;00m exception\n\u001b[0;32m    183\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m    184\u001b[0m     \u001b[39m# credit to\u001b[39;00m\n\u001b[0;32m    185\u001b[0m     \u001b[39m# https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/\u001b[39;00m\n\u001b[0;32m    186\u001b[0m     \u001b[39m# as the __traceback__ object creates a cycle\u001b[39;00m\n\u001b[0;32m    187\u001b[0m     \u001b[39mdel\u001b[39;00m exception, replace_context, from_, with_traceback\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\base.py:1276\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, *args)\u001b[0m\n\u001b[0;32m   1274\u001b[0m                 \u001b[39mbreak\u001b[39;00m\n\u001b[0;32m   1275\u001b[0m     \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m evt_handled:\n\u001b[1;32m-> 1276\u001b[0m         \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mdialect\u001b[39m.\u001b[39;49mdo_execute(\n\u001b[0;32m   1277\u001b[0m             cursor, statement, parameters, context\n\u001b[0;32m   1278\u001b[0m         )\n\u001b[0;32m   1280\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n\u001b[0;32m   1281\u001b[0m     \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdispatch\u001b[39m.\u001b[39mafter_cursor_execute(\n\u001b[0;32m   1282\u001b[0m         \u001b[39mself\u001b[39m,\n\u001b[0;32m   1283\u001b[0m         cursor,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m   1287\u001b[0m         context\u001b[39m.\u001b[39mexecutemany,\n\u001b[0;32m   1288\u001b[0m     )\n",
      "File \u001b[1;32md:\\myprojects\\gitee\\dash-flask-login\\venv\\lib\\site-packages\\sqlalchemy\\engine\\default.py:608\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[1;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[0;32m    607\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mdo_execute\u001b[39m(\u001b[39mself\u001b[39m, cursor, statement, parameters, context\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m):\n\u001b[1;32m--> 608\u001b[0m     cursor\u001b[39m.\u001b[39;49mexecute(statement, parameters)\n",
      "\u001b[1;31mIntegrityError\u001b[0m: (sqlite3.IntegrityError) UNIQUE constraint failed: user.email\n[SQL: INSERT INTO user (username, email, password) VALUES (?, ?, ?)]\n[parameters: ('test', 'test@test.com', 'sha256$56GMHIe8$b77d42b55c9fff8fe78b4aedba02ff2e280032642b4703417881bec64e28206f')]\n(Background on this error at: http://sqlalche.me/e/13/gkpj)"
     ]
    }
   ],
   "source": [
    "um.add_user('test','test1','test@test.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('test', 'test@test.com')\n"
     ]
    }
   ],
   "source": [
    "um.show_users()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "um.del_user('test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 ('venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "6cae6794da688ac6b6382698e31c8c30989e14e4d6c59cef756ea91c30a975f3"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
